<?php

namespace App\Models;

use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;

class Namecard extends Base
{
    protected $table = 'namecard';
    protected $hidden = ['updated_at','deleted_at'];
    use SoftDeletes;

    public function add_data($data){
        $result = $this->create($data)->id;
        if (!$result) {
            return false;
        }
        return $result;
    }

    public function getDataById($id){
        $result = $this
            ->where('id',hashid_decode($id))
            ->first();
        if (!$result) return false;
        unset($result['id']);
        unset($result['uid']);
        return $result;
    }

    public function getDataByMap($map,$pageSize){
        $result = $this
            ->orderBy('created_at','desc')
            ->paginate($pageSize)
            ->toArray();
        $data = $result['data'];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['id'] = hashid_encode($v['id']);
            }
            $result['data'] = $data;
        }
        return $result;
    }

    public function edit_data($id,$data){
        if(array_key_exists('code',$data)) unset($data['code']);
        if(array_key_exists('uid',$data)) unset($data['uid']);
        $result = $this->where('id',hashid_decode($id))->update($data);
        return $result;
    }

    public function delData($id){
        $result = $this->where('id',hashid_decode($id))->delete();
        return $result;
    }
}
